import random
import time

import pyautogui
import cv2
from PIL import ImageGrab
from utils import find_image
class Picker:
    left, top, right, bottom = [0, 0, 0, 0]
    last_teft, last_top = [0, 0];
    same_cnt = 1
    def __init__(self, left, top, right, bottom):
        self.left = left if (left >= 0) else 0
        self.top = top if (top >= 0) else 0
        self.right = right
        self.bottom = bottom
    def pick(self,):
        times = 5
        while times > 0:
            print("开始查找目标视图块")
            minLocal = self.checkBag()
            if (minLocal == -1 ):
                # self.around()
                times -= 1
            else:
                break
        if times == 0:
            return {"state" : "NOT_FIND"}
        if times != 0:
            print("找到了包裹")
        leftFirstLearn = self.left + minLocal[0] + 106
        centerBtnHeight = self.top + minLocal[1] + 72
        pyautogui.leftClick(leftFirstLearn, centerBtnHeight)#模拟鼠标指针 传送到指定坐标
        time.sleep(0.1)
        times = 6
        while times > 0:
            left = 451
            minLocal = self.checkTeacher(left)
            if (minLocal == -1 ):
                times -= 1
                print("没找到拜师按钮")
                break
            else:
                adjust2Center = 27
                if (minLocal[0] == self.last_teft or minLocal[1] == self.last_top):
                    if self.same_cnt == 2:
                        break
                else:
                    self.same_cnt = 1
                    self.last_teft = minLocal[0]
                    self.last_top = minLocal[1]
                pyautogui.moveTo(left + minLocal[0] + adjust2Center, centerBtnHeight, 0.2)#模拟鼠标指针 传送到指定坐标
                pyautogui.leftClick(left + minLocal[0] + adjust2Center , centerBtnHeight )#模拟鼠标指针 传送到指定坐标
                time.sleep(0.1 + self.rand())
        if times == 0:
            print("没找到拜师按钮")
            return {"state":"NOT_FIND"}
        return {"state":"NOT_FIND"}

    def checkBag(self):
        ImageGrab.grab((self.left, self.top, self.right, self.bottom)).save("static/images/img_Winapi.bmp")
        learn_block_img = cv2.imread("static/images/window_img.bmp")
        windowImg = cv2.imread("static/images/img_Winapi.bmp")
        minLocal = find_image.fastFind(windowImg, learn_block_img)
        return minLocal
    def checkTeacher(self, left = 451):
        ImageGrab.grab((left, self.top, self.right, self.bottom)).save("static/images/img_Winapi.bmp")
        grassImg = cv2.imread("static/images/learn_img.bmp")
        windowImg = cv2.imread("static/images/img_Winapi.bmp")
        minLocal = find_image.fastFind(windowImg, grassImg, 0.000000005)
        return minLocal
    def rand(self):
        return random.randrange(0, 10) / 100.0
    def skill_book2value(self):
        left = 300
        top = 500
        while True:
            minLocal = self.check_book(left, top)
            if (minLocal == -1 ):
                print("没找到提取按钮")
                break
            else:
                adjust2Center = 80
                adjust2CenterH = 10
                pyautogui.moveTo(left + minLocal[0] + adjust2Center, top + minLocal[1] + adjust2CenterH, 0.2)#模拟鼠标指针 传送到指定坐标
                pyautogui.leftClick(left + minLocal[0] + adjust2Center , top + minLocal[1] + adjust2CenterH )#模拟鼠标指针 传送到指定坐标
                time.sleep(0.1 + self.rand())

    def check_book(self, left, top):
        ImageGrab.grab((left, top, self.right, self.bottom)).save("static/images/img_Winapi.bmp")
        grassImg = cv2.imread("static/images/img_do_learn.bmp")
        windowImg = cv2.imread("static/images/img_Winapi.bmp")
        minLocal = find_image.fastFind(windowImg, grassImg)
        return minLocal